var util = require('/utils/url.js');
// const https = require('./utils/https.js');
var url = util.url
App({
    onLaunch: function (options) {
        if (options) {
            if (options.scene == 1047) {
                var scene = decodeURIComponent(options.query.scene);
                var scenearr = scene.split("&");
                wx.setStorageSync('l_id', scenearr[0]);
                wx.setStorageSync('timestr', scenearr[1]);
            } else if (options.query['l_id'] > 0) {
                wx.setStorageSync('l_id', options.query['l_id']);
                if (options.query['timestr'] > 0) {
                    wx.setStorageSync('timestr', options.query['timestr']);
                }
            }
        }
        // 展示本地存储能力
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        // 设置分享
        this.overShare()
    },
    globalData: {
        userInfo: null,
        version: null,
        cart_id: null,
        address_id: 0,
        use_integral: 0.00,
        user_coupon_id: 0.00,
        invoice: '',
        order_sn: '',
        order_type: '',
        token: '',
        promote_user_id: '', // 推荐人
        promote_qrcode_url: '', // 推广二维码
    },
    showToast(e, success) {
        wx.showToast({
            title: e,
            icon: 'none',
            mask: true,
            duration: 1500,
            success: () => {
                if (success) {
                    setTimeout(() => {
                        success()
                    }, 500)
                }
            }
        })
    },
    showSuccessToast(e, success) {
        wx.showToast({
            title: e,
            mask: true,
            duration: 1500,
            success: () => {
                if (success) {
                    setTimeout(() => {
                        success()
                    }, 500)
                }
            }
        })
    },

    showModal(title, content, success) {
        wx.showModal({
            title: title,
            content: content,
            confirmColor: this.globalData.diy_color.primary_hex,
            success: (res) => {
                if (res.confirm) {
                    success()
                }
            }
        })
    },
    // 全局的分享
    overShare: function () {
        //监听路由切换
        //间接实现全局设置分享内容
        wx.onAppRoute( (res) => {
            //获取加载的页面
            let pages = getCurrentPages(),
                //获取当前页面的对象
                view = pages[pages.length - 1],
                data;
            if (view) {
                data = view.data;
                var that = this;
                if (!data.isOverShare) {
                    if (this.globalData.promote_qrcode_url) {
                        view.onShareAppMessage =  () => {
                            //你的分享配置
                            return {
                                title: '魔尺验房',
                                path: '/pages/home/home',
                                imageUrl: that.globalData.promote_qrcode_url
                            };
                        }
                    } else {

                    }
                    const user_id = wx.getStorageSync('user_id');
                    if(!user_id) return
                    wx.request({
                        url: 'https://wx.amagich.com/' + 'v1/' + 'getPromoteQrCode',
                        data: {
                            user_id: user_id,
                        },
                        header: {
                            "Content-Type": "application/json"
                        },
                        method: 'GET',
                        success: res => {
                            if (res.data.code == 200) {
                                console.log(res.data.resultData.promote_qrcode_url)
                                if(res.data.resultData.promote_qrcode_url) {
                                    this.globalData.promote_qrcode_url = res.data.resultData.promote_qrcode_url
                                    view.onShareAppMessage = function () {
                                        console.log('here share')
                                        //你的分享配置
                                        return {
                                            title: '魔尺验房',
                                            path: '/pages/home/home',
                                            imageUrl: res.data.resultData.promote_qrcode_url
                                        };
                                    }
                                }
                            }
                        },
                        fail: res => {
                        },
                        complete: () => {
                        }
                    })
                }
            }
        })
    }
})
